﻿using System;
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Data;
using CRM.MODEL;
using CRM.UTIL;

namespace CRM.DAL
{
    public class PartsModelsD : DBChannel
    {
        /// <summary>
        /// 可以在此选择数据库连接，默认为CRM
        /// </summary>
        public PartsModelsD() : base()
        {
        }


        #region 执行存储过程 Pro_GetPartsModels ,根据条件获取回馈信息

        /// <summary>
        /// 执行存储过程 Pro_GetPartsModels 
        ///xialei
        /// </summary>

        public DataTable ExecuteGetPartsModels(ref CRM.MODEL.GetPartsModels stu)
        {
            //存储过程参数	 		     
            this.sqlCmd.Parameters.Add(new SqlParameter(CRM.MODEL.GetPartsModels.IDPARM, SqlDbType.Int));
            this.sqlCmd.Parameters.Add(new SqlParameter(CRM.MODEL.GetPartsModels.BRANDSIDPARM, SqlDbType.Int));
            this.sqlCmd.Parameters.Add(new SqlParameter(CRM.MODEL.GetPartsModels.MODELNAMEPARM, SqlDbType.VarChar, 100));
            this.sqlCmd.Parameters.Add(new SqlParameter(CRM.MODEL.GetPartsModels.MODELDISPLAYNAMEPARM, SqlDbType.VarChar,
                100));
            this.sqlCmd.Parameters.Add(new SqlParameter(CRM.MODEL.GetPartsModels.ISAVAILABLEPARM, SqlDbType.Bit));
            this.sqlCmd.Parameters.Add(new SqlParameter(CRM.MODEL.GetPartsModels.CREATEBYPARM, SqlDbType.VarChar, 20));
            this.sqlCmd.Parameters.Add(new SqlParameter(CRM.MODEL.GetPartsModels.CREATETIMEPARM, SqlDbType.DateTime));
            this.sqlCmd.Parameters.Add(new SqlParameter(CRM.MODEL.GetPartsModels.LASTMODIFYBYPARM, SqlDbType.VarChar, 20));
            this.sqlCmd.Parameters.Add(new SqlParameter(CRM.MODEL.GetPartsModels.LASTMODIFYTIMEPARM, SqlDbType.DateTime));
            this.sqlCmd.Parameters.Add(new SqlParameter(CRM.MODEL.GetPartsModels.KEYVALUEPARM, SqlDbType.VarChar));
            this.sqlCmd.Parameters.Add(new SqlParameter(CRM.MODEL.GetPartsModels.PARTSIDPARM, SqlDbType.Int));

            //分页参数
            this.sqlCmd.Parameters.Add(new SqlParameter(CRM.MODEL.GetPartsModels.ORDERSTRING_PARM, SqlDbType.VarChar,
                1000));
            this.sqlCmd.Parameters.Add(new SqlParameter(CRM.MODEL.GetPartsModels.PAGESIZE_PARM, SqlDbType.Int));
            this.sqlCmd.Parameters.Add(new SqlParameter(CRM.MODEL.GetPartsModels.CURRENTPAGEINDEX_PARM, SqlDbType.Int));
            this.sqlCmd.Parameters.Add(new SqlParameter(CRM.MODEL.GetPartsModels.TOTALITEMS_PARM, SqlDbType.Decimal));

            //设置参数的值
            this.sqlCmd.Parameters[CRM.MODEL.GetPartsModels.IDPARM].Value = stu.IDVALUE;
            this.sqlCmd.Parameters[CRM.MODEL.GetPartsModels.BRANDSIDPARM].Value = stu.BRANDSIDVALUE;
            this.sqlCmd.Parameters[CRM.MODEL.GetPartsModels.MODELNAMEPARM].Value = stu.MODELNAMEVALUE;
            this.sqlCmd.Parameters[CRM.MODEL.GetPartsModels.MODELDISPLAYNAMEPARM].Value = stu.MODELDISPLAYNAMEVALUE;
            this.sqlCmd.Parameters[CRM.MODEL.GetPartsModels.ISAVAILABLEPARM].Value = stu.ISAVAILABLEVALUE;
            this.sqlCmd.Parameters[CRM.MODEL.GetPartsModels.CREATEBYPARM].Value = stu.CREATEBYVALUE;
            this.sqlCmd.Parameters[CRM.MODEL.GetPartsModels.CREATETIMEPARM].Value = stu.CREATETIMEVALUE;
            this.sqlCmd.Parameters[CRM.MODEL.GetPartsModels.LASTMODIFYBYPARM].Value = stu.LASTMODIFYBYVALUE;
            this.sqlCmd.Parameters[CRM.MODEL.GetPartsModels.LASTMODIFYTIMEPARM].Value = stu.LASTMODIFYTIMEVALUE;
            this.sqlCmd.Parameters[CRM.MODEL.GetPartsModels.KEYVALUEPARM].Value = stu.KEYVALUEVALUE;
            this.sqlCmd.Parameters[CRM.MODEL.GetPartsModels.PARTSIDPARM].Value = stu.PARTSIDVALUE;

            //设置分页参数的值
            this.sqlCmd.Parameters[CRM.MODEL.GetPartsModels.ORDERSTRING_PARM].Value = stu.ORDERSTRING_VALUE;
            this.sqlCmd.Parameters[CRM.MODEL.GetPartsModels.PAGESIZE_PARM].Value = stu.PAGESIZE_VALUE;
            this.sqlCmd.Parameters[CRM.MODEL.GetPartsModels.CURRENTPAGEINDEX_PARM].Value = stu.CURRENTPAGEINDEX_VALUE;
            this.sqlCmd.Parameters[CRM.MODEL.GetPartsModels.TOTALITEMS_PARM].Value = stu.TOTALITEMS_VALUE;
            this.sqlCmd.Parameters[CRM.MODEL.GetPartsModels.TOTALITEMS_PARM].Direction = ParameterDirection.Output;

            //执行
            DataTable dt = this.GetDataByProcedure("dbo.Pro_GetPartsModels");
            //获取数据库的信息
            stu.TOTALITEMS_VALUE = this.sqlCmd.Parameters[CRM.MODEL.GetPartsModels.TOTALITEMS_PARM].Value;

            return dt;
        }

        #endregion

        #region 执行存储过程 Pro_EditPartsModels,根据条件获取回馈信息

        /// <summary>
        /// 执行存储过程 Pro_EditPartsModels 的相关参数
        ///xialei
        /// </summary>
        public int ExecuteEditPartsModels(ref CRM.MODEL.EditPartsModels stu)
        {
            if ((string) stu.ACTION_VALUE == "Update")
            {
                #region Update

                var sqlSb = new StringBuilder();
                sqlCmd.Parameters.Clear();
                sqlSb.Append(" UPDATE PartsModels SET ");

                if (stu.ID_VALUE != DBNull.Value)
                {
                }
                if (stu.BRANDSID_VALUE != DBNull.Value)
                {

                    sqlSb.Append(" BrandsID=@BrandsID,");
                    sqlCmd.Parameters.Add(new SqlParameter("@BRANDSID", stu.BRANDSID_VALUE));
                }
                if (stu.MODELNAME_VALUE != DBNull.Value)
                {

                    sqlSb.Append(" ModelName=@ModelName,");
                    sqlCmd.Parameters.Add(new SqlParameter("@MODELNAME", stu.MODELNAME_VALUE));
                }
                if (stu.MODELDISPLAYNAME_VALUE != DBNull.Value)
                {

                    sqlSb.Append(" ModelDisplayName=@ModelDisplayName,");
                    sqlCmd.Parameters.Add(new SqlParameter("@MODELDISPLAYNAME", stu.MODELDISPLAYNAME_VALUE));
                }
                if (stu.ISAVAILABLE_VALUE != DBNull.Value)
                {

                    sqlSb.Append(" IsAvailable=@IsAvailable,");
                    sqlCmd.Parameters.Add(new SqlParameter("@ISAVAILABLE", stu.ISAVAILABLE_VALUE));
                }
                if (stu.CREATEBY_VALUE != DBNull.Value)
                {

                    sqlSb.Append(" CreateBy=@CreateBy,");
                    sqlCmd.Parameters.Add(new SqlParameter("@CREATEBY", stu.CREATEBY_VALUE));
                }
                if (stu.CREATETIME_VALUE != DBNull.Value)
                {

                    sqlSb.Append(" CreateTime=@CreateTime,");
                    sqlCmd.Parameters.Add(new SqlParameter("@CREATETIME", stu.CREATETIME_VALUE));
                }
                if (stu.LASTMODIFYBY_VALUE != DBNull.Value)
                {

                    sqlSb.Append(" LastModifyBy=@LastModifyBy,");
                    sqlCmd.Parameters.Add(new SqlParameter("@LASTMODIFYBY", stu.LASTMODIFYBY_VALUE));
                }
                if (stu.LASTMODIFYTIME_VALUE != DBNull.Value)
                {

                    sqlSb.Append(" LastModifyTime=@LastModifyTime,");
                    sqlCmd.Parameters.Add(new SqlParameter("@LASTMODIFYTIME", stu.LASTMODIFYTIME_VALUE));
                }

                var sql = sqlSb.ToString().TrimEnd(',');

                sqlCmd.Parameters.Add(new SqlParameter("@ID", stu.ID_VALUE));
                //sqlCmd.CommandText = sqlSb.ToString();
                //sqlCmd.ExecuteNonQuery();
                int i = base.ExecuteSQL(sql + " WHERE ID=@ID");

                return i;

                #endregion
            }
            else
            {
                //动作参数
                this.sqlCmd.Parameters.Add(new SqlParameter(CRM.MODEL.EditPartsModels.ACTION_PARM, SqlDbType.VarChar, 20));
                //错误信息参数
                this.sqlCmd.Parameters.Add(new SqlParameter(CRM.MODEL.EditPartsModels.ERRMSG_PARM, SqlDbType.VarChar,
                    100));
                this.sqlCmd.Parameters.Add(new SqlParameter(CRM.MODEL.EditPartsModels.OUTID_PARM, SqlDbType.Int));
                ////表字段参数
                this.sqlCmd.Parameters.Add(new SqlParameter(CRM.MODEL.EditPartsModels.IDPARM, SqlDbType.Int));
                this.sqlCmd.Parameters.Add(new SqlParameter(CRM.MODEL.EditPartsModels.BRANDSIDPARM, SqlDbType.Int));
                this.sqlCmd.Parameters.Add(new SqlParameter(CRM.MODEL.EditPartsModels.MODELNAMEPARM, SqlDbType.VarChar,
                    100));
                this.sqlCmd.Parameters.Add(new SqlParameter(CRM.MODEL.EditPartsModels.MODELDISPLAYNAMEPARM,
                    SqlDbType.VarChar, 100));
                this.sqlCmd.Parameters.Add(new SqlParameter(CRM.MODEL.EditPartsModels.ISAVAILABLEPARM, SqlDbType.Bit));
                this.sqlCmd.Parameters.Add(new SqlParameter(CRM.MODEL.EditPartsModels.CREATEBYPARM, SqlDbType.VarChar,
                    20));
                this.sqlCmd.Parameters.Add(new SqlParameter(CRM.MODEL.EditPartsModels.CREATETIMEPARM, SqlDbType.DateTime));
                this.sqlCmd.Parameters.Add(new SqlParameter(CRM.MODEL.EditPartsModels.LASTMODIFYBYPARM,
                    SqlDbType.VarChar, 20));
                this.sqlCmd.Parameters.Add(new SqlParameter(CRM.MODEL.EditPartsModels.LASTMODIFYTIMEPARM,
                    SqlDbType.DateTime));



                this.sqlCmd.Parameters[CRM.MODEL.EditPartsModels.ACTION_PARM].Value = stu.ACTION_VALUE;
                this.sqlCmd.Parameters[CRM.MODEL.EditPartsModels.ERRMSG_PARM].Direction = ParameterDirection.Output;
                this.sqlCmd.Parameters[CRM.MODEL.EditPartsModels.OUTID_PARM].Direction = ParameterDirection.Output;
                //设置参数的值
                this.sqlCmd.Parameters[CRM.MODEL.EditPartsModels.IDPARM].Value = stu.ID_VALUE;
                this.sqlCmd.Parameters[CRM.MODEL.EditPartsModels.BRANDSIDPARM].Value = stu.BRANDSID_VALUE;
                this.sqlCmd.Parameters[CRM.MODEL.EditPartsModels.MODELNAMEPARM].Value = stu.MODELNAME_VALUE;
                this.sqlCmd.Parameters[CRM.MODEL.EditPartsModels.MODELDISPLAYNAMEPARM].Value =
                    stu.MODELDISPLAYNAME_VALUE;
                this.sqlCmd.Parameters[CRM.MODEL.EditPartsModels.ISAVAILABLEPARM].Value = stu.ISAVAILABLE_VALUE;
                this.sqlCmd.Parameters[CRM.MODEL.EditPartsModels.CREATEBYPARM].Value = stu.CREATEBY_VALUE;
                this.sqlCmd.Parameters[CRM.MODEL.EditPartsModels.CREATETIMEPARM].Value = stu.CREATETIME_VALUE;
                this.sqlCmd.Parameters[CRM.MODEL.EditPartsModels.LASTMODIFYBYPARM].Value = stu.LASTMODIFYBY_VALUE;
                this.sqlCmd.Parameters[CRM.MODEL.EditPartsModels.LASTMODIFYTIMEPARM].Value = stu.LASTMODIFYTIME_VALUE;

                int i = this.ExecuteProcedure("dbo.Pro_EditPartsModels"); //执行	
                //获取数据库的信息
                stu.ERRMSG_VALUE = this.sqlCmd.Parameters[CRM.MODEL.EditPartsModels.ERRMSG_PARM].Value;
                return i;
            }
        }

        #endregion
    }
}